package com.wwwarehouse.taskcenter.interfaces.impl;

import com.wwwarehouse.taskcenter.bean.KeyBoardItemBean;
import com.wwwarehouse.taskcenter.interfaces.XmlParse;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.io.InputStream;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

/**
 * Created by clannad.shao on 2017/11/15.
 * Description:
 */

public class DomParse implements XmlParse<KeyBoardItemBean> {


    @Override
    public List<KeyBoardItemBean> parse(InputStream is) throws Exception {
        List<KeyBoardItemBean> keyBoardItems = new ArrayList<>();
        //取得DocumentBuilderFactory实例
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        //从factory获取DocumentBuilder实例
        DocumentBuilder builder = factory.newDocumentBuilder();
        //解析输入流 得到Document实例
        Document doc = builder.parse(is);
        Element rootElement = doc.getDocumentElement();
        NodeList items = rootElement.getElementsByTagName("keyitem");
        for (int i = 0; i < items.getLength(); i++) {
            KeyBoardItemBean keyBoardItem = new KeyBoardItemBean();
            Node item = items.item(i);
            NodeList properties = item.getChildNodes();
            for (int j = 0; j < properties.getLength(); j++) {
                Node property = properties.item(j);
                String nodeName = property.getNodeName();
                if (nodeName.equals("code")) {
                    keyBoardItem.setCode(property.getFirstChild().getNodeValue());
                } else if (nodeName.equals("name")) {
                    keyBoardItem.setName(property.getFirstChild().getNodeValue());
                }
            }
            keyBoardItems.add(keyBoardItem);
        }
        return keyBoardItems;
    }

    @Override
    public String serialize(List<KeyBoardItemBean> keyBoardItems) throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.newDocument();   //由builder创建新文档

        Element rootElement = doc.createElement("keyitems");

        for (KeyBoardItemBean item : keyBoardItems) {
            Element bookElement = doc.createElement("keyitem");
            bookElement.setAttribute("code", item.getCode() + "");

            Element nameElement = doc.createElement("name");
            nameElement.setTextContent(item.getName());
            bookElement.appendChild(nameElement);

            rootElement.appendChild(bookElement);
        }

        doc.appendChild(rootElement);

        //取得TransformerFactory实例
        TransformerFactory transFactory = TransformerFactory.newInstance();
        //从transFactory获取Transformer实例
        Transformer transformer = transFactory.newTransformer();
        // 设置输出采用的编码方式
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        // 是否自动添加额外的空白
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        // 是否忽略XML声明
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");

        StringWriter writer = new StringWriter();

        //表明文档来源是doc
        Source source = new DOMSource(doc);
        //表明目标结果为writer
        Result result = new StreamResult(writer);
        //开始转换
        transformer.transform(source, result);

        return writer.toString();
    }

}
